package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.common.VoDataUtil;
import cn.yougou.dto.Dto;

import cn.yougou.pojo.YgUserCoupons;
import cn.yougou.service.YgUserCouponsService;
import cn.yougou.service.YgCouponRules;
import cn.yougou.service.shopcart.YgCouponsService;
import cn.yougou.vo.YgGetCouponListVo;
import cn.yougou.vo.YgResCommodityVo;
import cn.yougou.vo.YgResOrderVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: DELL
 * @date: 2020/12/28 9:02
 * @description:
 */
@RestController
@RequestMapping("shopCoupon")
public class YgCouponsShopController {
    @Reference
    private YgCouponsService ygCouponsService;
    @Reference
    private YgUserCouponsService ygUserCouponsService;
    @Reference
    private YgCouponRules YgCouponRules;
    @Autowired
    private VoDataUtil voDataUtil;
    @GetMapping("getCouponList")
    public Dto getCouponList(Integer ygCommodityid,Integer ygCategoryid,Integer ygUserid){
        List list =ygCouponsService.getCouponList( ygCommodityid, ygCategoryid, ygUserid);
        return DtoUtil.returnDataSuccess(list);
    }
    @PostMapping("createCoupon")
    public Dto createCoupon(@RequestBody YgUserCoupons ygUserCoupons){
        Integer result = ygCouponsService.createCoupon(ygUserCoupons.getCouponsid(),ygUserCoupons.getUserid() );
        if(result !=null){
            if(result==2){
                return DtoUtil.returnSuccess("200");
            }else{
                return DtoUtil.returnSuccess("4001");
            }
        }else{
            return DtoUtil.returnSuccess("4001");
        }

    }
    //Integer userId, YgResCommodityVo[] ygResCommodityVos
    @PostMapping ("getUserCouponList")
    public Dto getUserCouponList(@RequestBody YgResOrderVo ygResOrderVo){
        ygResOrderVo.setYgResCommodityVos(voDataUtil.YgCartTransformYgResCommodityVo(ygResOrderVo.getYgCarts())) ;
        //这行代码的意义是 对应商品满足优惠券规则后返回一个 对应的 json字符串和优惠金额
        List<YgGetCouponListVo> userCouponList = ygUserCouponsService.getUserCouponList(ygResOrderVo.getUserid(), ygResOrderVo);
        if(userCouponList!=null){
            return DtoUtil.returnDataSuccess(userCouponList);
        }else{
            return DtoUtil.returnSuccess("出现未知错误请联系管理员",new Dto<>());
        }
    }

    /**
     * 计算 优惠价格 与 实际价格
     * 将商品价格 与优惠价格计算出并返回
     * */
    @PostMapping("calculateMoney")
    public Dto calculateMoney(@RequestBody YgResOrderVo ygResOrderVo){
        ygResOrderVo.setYgResCommodityVos(voDataUtil.YgCartTransformYgResCommodityVo(ygResOrderVo.getYgCarts())) ;
        YgResOrderVo ygResOrderVo1 = ygUserCouponsService.calculateMoney(ygResOrderVo);
        if(ygResOrderVo1!=null){
            return DtoUtil.returnDataSuccess(ygResOrderVo1);
        }else{
            return DtoUtil.returnSuccess("出现未知错误请联系管理员",new Dto<>());
        }
    }
}
